/**
 * 
 */
package com.vincent.action.web;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.vincent.action.basic.BasicAction;
import com.vincent.pojo.User;
import com.vincent.service.LoginService;
import com.zhj.util.UTIL;

/**
 * 作者： Vincent
 * 创建日期： 2014年4月23日 下午7:10:01
 * 项目名字： MyForum
 * 文件名字： LoginAction.java
 */
@Component("loginAction")
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/")
public class LoginAction extends BasicAction{
	
	private String userName;

	private String password;

	private String authCode;
	@Resource
	private LoginService loginService;
	
	private static Logger logger = Logger.getLogger(LoginAction.class);
	
	private ByteArrayInputStream inputStream;

	@Action(value="login",results={@Result(name="ok",location="/WEB-INF/web/login.jsp")})
	public String login(){
		
		return "ok";
	}
	
	/**
	 * 登陆页面验证码
	 * 
	 * @return
	 * @throws IOException
	 */
	@Action(value = "loginAuthCodeImage", results = { @Result(name = "authCodeImage", params = { "contentType",
			"image/jpeg", "inputName", "inputStream", "bufferSize", "2048" }, type = "stream") })
	public String loginAuthCodeImage() throws IOException {
		String authCode = UTIL.getAuthCode(5);

		logger.info("[登陆验证码：" + authCode + "]");

		session.setAttribute("enrollAuthCode", authCode);

		BufferedImage img = UTIL.getCodeImage(authCode);

		ByteArrayOutputStream output = new ByteArrayOutputStream();

		ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);

		ImageIO.write(img, "JPEG", imageOut);

		imageOut.close();

		ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());

		this.setInputStream(input);

		return "authCodeImage";
	}

	
	/**
	 * 登陆验证
	 * 
	 * @return
	 */
	@Action(value = "loginCheck", results = { @Result(name = "ok", type = "redirectAction", params = { "namespace",
			"/", "actionName", "index" }) })
	public String loginCheck() {

		if (request.getMethod().equals("GET")) {

			try {

				response.sendRedirect("login.html?error=URLError");

			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error(e);
			}
			return null;
		}

		if (!authCode.equalsIgnoreCase(((String) (session.getAttribute("enrollAuthCode"))))) {

			try {
				response.sendRedirect("login.html?error=authCodeError");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error(e);
			}
			return null;
		}

		HashMap<String, Object> hMap = loginService.getLoginCheck(userName, password);

		if (hMap.get("key").equals("error")) {
			try {
				response.sendRedirect("login.html?error=" + hMap.get("data"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error(e);
			}
			return null;
		}

		if(hMap.get("key").equals("ok")){
			User user = (User) hMap.get("data");
			session.setAttribute("user", user);
			return "ok";
		}
		return null;
	}
	
	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}
}
